﻿
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows.Controls;
using WCYCReader.Utility;
namespace WCYCReader.Main.Core
{
    public class QQLineBreaker
    {
        public static string specialC_1;

        public static string specialC_2;

        public static string specialC_3;

        public static string specialC_4;

        public static string specialC_5;

        public static string specialC_6;

        public static string specialC_7;

        public static string specialC_8;

        public static char[] PUNCTUATION_CHARS;

        public static char[] PUNCTUATION_MIDDLE_CHARS;

        public static char[] PUNCTUATION_LEFT_CHARS;

        public static char[] NEWLINE_SPECIAL_CHARS;

        public static char[] ADD_LINE_END_CHARS;

        public static int curFontSize;

        static QQLineBreaker()
        {
            QQLineBreaker.specialC_1 = "";
            QQLineBreaker.specialC_2 = "";
            QQLineBreaker.specialC_3 = "";
            QQLineBreaker.specialC_4 = "";
            QQLineBreaker.specialC_5 = "\t";
            QQLineBreaker.specialC_6 = "";
            QQLineBreaker.specialC_7 = "";
            QQLineBreaker.specialC_8 = "";
            QQLineBreaker.PUNCTUATION_CHARS = new char[] { '。', '，', ',', '.', '！', '!', '？', '?', '、', '”', '》', '）', '：', '…', '；', ';', '’' };
            QQLineBreaker.PUNCTUATION_MIDDLE_CHARS = new char[] { '，', '！', '!', ',', '.', '？', '?', '》', '）', '：', '；', ';' };
            QQLineBreaker.PUNCTUATION_LEFT_CHARS = new char[] { '。', '、' };
            QQLineBreaker.NEWLINE_SPECIAL_CHARS = new char[] { '“', '（', '《', '‘' };
            QQLineBreaker.ADD_LINE_END_CHARS = new char[] { '”', '’' };
            QQLineBreaker.curFontSize = 23;
        }

        public QQLineBreaker()
        {
        }

        protected static void addLine(List<string> inputLines, string line)
        {
            inputLines.Add(line);
        }

        public static void breakText(IBookBuff buff, int maxWidth)
        {
            float[] singleArray;
            List<float[]> singleArrays = buff.mLinesNum;
            List<float[]> singleArrays1 = buff.mLineXY;
            List<string> strs = buff.mLines;
            List<float[]> singleArrays2 = new List<float[]>();
            string str = buff.mText;
            string[] specialC1 = new string[] { QQLineBreaker.specialC_1, "|", QQLineBreaker.specialC_2, "|", QQLineBreaker.specialC_3, "|", QQLineBreaker.specialC_4, "|", QQLineBreaker.specialC_5, "|", QQLineBreaker.specialC_6, "|", QQLineBreaker.specialC_7, "|", QQLineBreaker.specialC_8 };
            Regex regex = new Regex(string.Concat(specialC1));
            regex.Replace(str, " ");
            int length = str.Length;
            str = str.Replace('?', '？');
            int num = str.Length;
            int num1 = -1;
            float widthChar = 0f;
            bool flag = false;
            int num2 = 0;
            List<float[]> singleArrays3 = new List<float[]>();
            bool flag1 = false;
            bool flag2 = false;
            if (buff.mStartPos == (long)0)
            {
                flag2 = true;
            }
            int length1 = str.Length;
            while (num2 < str.Length)
            {
                if (num1 == -1)
                {
                    num1 = num2;
                }
                flag = false;
                char chr = str[num2];
                float single = QQLineBreaker.getWidthChar(chr);
                if (num2 < str.Length - 1 && chr == '。' && str[num2 + 1] == '”')
                {
                    single = single / 2f;
                }
                bool flag3 = false;
                bool flag4 = false;
                if (flag1 || flag2)
                {
                    if (flag2)
                    {
                        while ((chr == ' ' || chr == '\u3000' || chr == '\u00A0') && num2 < str.Length - 1)
                        {
                            singleArray = new float[] { widthChar, default(float) };
                            singleArrays3.Add(singleArray);
                            int num3 = num2 + 1;
                            num2 = num3;
                            chr = str[num3];
                        }
                    }
                    flag2 = false;
                    if (chr != ' ' && chr != '\u3000')
                    {
                        widthChar = widthChar + QQLineBreaker.getWidthChar('\u4E2D') * 2f;
                    }
                    flag1 = false;
                }
                singleArray = new float[] { widthChar, default(float) };
                singleArrays3.Add(singleArray);
                if (chr == '\n' || chr == '\r' || chr == '\u2029')
                {
                    flag1 = true;
                    bool flag5 = true;
                    while ((chr == '\n' || chr == '\r' || chr == '\u2029' || chr == ' ' || chr == '\u3000' || chr == '\u00A0') && num2 < str.Length - 1)
                    {
                        int num4 = num2 + 1;
                        num2 = num4;
                        chr = str[num4];
                        flag5 = false;
                        singleArray = new float[] { widthChar, default(float) };
                        singleArrays3.Add(singleArray);
                    }
                    if (num2 == str.Length - 1)
                    {
                        flag5 = true;
                    }
                    if (flag5)
                    {
                        num2++;
                        singleArray = new float[] { widthChar, default(float) };
                        singleArrays3.Add(singleArray);
                    }
                    singleArray = new float[] { (float)num1, (float)num2, default(float) };
                    singleArrays.Add(singleArray);
                    string str1 = str.Substring(num1, num2 - num1);
                    (new Regex("\r|\n|\u2029")).Replace(str1, " ");
                    char[] charArray = str1.ToCharArray();
                    bool flag6 = false;
                    for (int i = 0; i < (int)charArray.Length; i++)
                    {
                        if (charArray[i] == '\u3000' || charArray[i] == '\u00A0')
                        {
                            charArray[i] = ' ';
                            flag6 = true;
                        }
                    }
                    if (flag6)
                    {
                        str1 = new string(charArray);
                    }
                    num2--;
                    if (str1.Trim().Length > 0)
                    {
                        QQLineBreaker.addLine(strs, str1);
                        singleArrays3.RemoveAt(singleArrays3.Count - 1);
                        float[] singleArray1 = new float[str1.Length << 1];
                        int num5 = 0;
                        foreach (float[] singleArray2 in singleArrays3)
                        {
                            singleArray1[num5] = singleArray2[0];
                            singleArray1[num5 + 1] = singleArray2[1];
                            num5 = num5 + 2;
                        }
                        singleArrays1.Add(singleArray1);
                    }
                }
                else if (widthChar + single > (float)maxWidth)
                {
                    flag3 = true;
                    bool flag7 = false;
                    if (chr > ' ' && chr < '\u007F')
                    {
                        char chr1 = '\0';
                        float single1 = widthChar;
                        singleArrays2.Clear();
                        int num6 = num2 - 1;
                        while (num6 > num1)
                        {
                            chr1 = str[num6];
                            if (chr1 <= ' ' || chr1 >= '\u007F')
                            {
                                num2 = num6 + 1;
                                if (chr1 == ' ')
                                {
                                    flag = true;
                                }
                                flag7 = true;
                                break;
                            }
                            else
                            {
                                widthChar = widthChar - QQLineBreaker.getWidthChar(chr1);
                                float[] item = singleArrays3[singleArrays3.Count - 1];
                                singleArrays3.RemoveAt(singleArrays3.Count - 1);
                                singleArrays2.Insert(0, item);
                                num6--;
                            }
                        }
                        if (!flag7)
                        {
                            widthChar = single1;
                            singleArrays3.InsertRange(singleArrays3.Count - 1, singleArrays2);
                            singleArrays2.Clear();
                        }
                    }
                    if (flag7)
                    {
                        chr = str[num2];
                        single = QQLineBreaker.getWidthChar(chr);
                    }
                    if (QQLineBreaker.isPunctuationChars(chr) && widthChar + single > (float)maxWidth)
                    {
                        num2++;
                        flag4 = true;
                    }
                    if (flag4)
                    {
                        float single2 = widthChar + single - (float)maxWidth;
                        bool flag8 = false;
                        if (num2 < str.Length && QQLineBreaker.isPunctuationChars(str[num2]))
                        {
                            flag8 = true;
                        }
                        if (single2 > 0f && flag8 || single2 > single / 2f)
                        {
                            bool flag9 = true;
                            if (num2 < str.Length)
                            {
                                flag9 = QQLineBreaker.isAddLineEndSpecialChars(str[num2]);
                            }
                            if (!flag9 || single2 > single / 2f)
                            {
                                int num7 = num2;
                                float item1 = widthChar;
                                List<float[]> singleArrays4 = new List<float[]>();
                                float[] item2 = singleArrays3[singleArrays3.Count - 1];
                                singleArrays3.RemoveAt(singleArrays3.Count - 1);
                                singleArrays4.Insert(0, item2);
                                num7 = num7 - 2;
                                bool flag10 = false;
                                while (num7 > num1)
                                {
                                    char chr2 = str[num7];
                                    item1 = singleArrays3[num7 - num1][0];
                                    if (QQLineBreaker.isPunctuationChars(chr2))
                                    {
                                        float[] item3 = singleArrays3[singleArrays3.Count - 1];
                                        singleArrays3.RemoveAt(singleArrays3.Count - 1);
                                        singleArrays4.Insert(0, item3);
                                        num7--;
                                    }
                                    else
                                    {
                                        flag10 = true;
                                        num2 = num7;
                                        widthChar = item1;
                                        flag4 = false;
                                        break;
                                    }
                                }
                                if (!flag10)
                                {
                                    num2--;
                                    flag4 = false;
                                    singleArrays3.InsertRange(singleArrays3.Count - 1, singleArrays4);
                                }
                            }
                        }
                    }
                    if (flag4)
                    {
                        flag4 = false;
                        bool flag11 = false;
                        if (num2 < str.Length && QQLineBreaker.isPunctuationChars(str[num2]))
                        {
                            flag11 = true;
                        }
                        if (QQLineBreaker.isPunctuationMiddleChars(chr))
                        {
                            single = single / 4f * 3f;
                        }
                        else if (QQLineBreaker.isPunctuationLeftChars(chr))
                        {
                            single = single / 2f;
                        }
                        float single3 = widthChar + single - (float)maxWidth;
                        if (single3 <= 0f || flag11)
                        {
                            num2--;
                            flag3 = false;
                        }
                        else
                        {
                            float single4 = -single3 / (float)(num2 - num1 - 1);
                            singleArray = new float[] { (float)num1, (float)num2, single4 };
                            singleArrays.Add(singleArray);
                            string str2 = str.Substring(num1, num2 - num1);
                            QQLineBreaker.addLine(strs, str2);
                            num2--;
                            float[] singleArray3 = new float[str2.Length << 1];
                            int num8 = 0;
                            float single5 = 0f;
                            foreach (float[] singleArray4 in singleArrays3)
                            {
                                singleArray3[num8] = singleArray4[0] + single5;
                                singleArray3[num8 + 1] = singleArray4[1];
                                num8 = num8 + 2;
                                single5 = single5 + single4;
                            }
                            singleArrays1.Add(singleArray3);
                        }
                    }
                    else if (str[num2] == ' ' || !flag)
                    {
                        if (num2 > 0)
                        {
                            for (char j = str[num2 - 1]; QQLineBreaker.isNewLineSpecialChars(j); j = str[num2 - 1])
                            {
                                singleArrays3.RemoveAt(singleArrays3.Count - 1);
                                num2--;
                                widthChar = widthChar - QQLineBreaker.getWidthChar(j);
                                if (num2 <= 0)
                                {
                                    break;
                                }
                            }
                        }
                        float single6 = ((float)maxWidth - widthChar) / (float)(num2 - num1 - 1);
                        singleArray = new float[] { (float)num1, (float)num2, single6 };
                        singleArrays.Add(singleArray);
                        int length2 = str.Length;
                        string str3 = str.Substring(num1, num2 - num1);
                        QQLineBreaker.addLine(strs, str3);
                        num2--;
                        singleArrays3.RemoveAt(singleArrays3.Count - 1);
                        float[] singleArray5 = new float[str3.Length << 1];
                        int num9 = 0;
                        float single7 = 0f;
                        foreach (float[] singleArray6 in singleArrays3)
                        {
                            singleArray5[num9] = singleArray6[0] + single7;
                            singleArray5[num9 + 1] = singleArray6[1];
                            num9 = num9 + 2;
                            single7 = single7 + single6;
                        }
                        singleArrays1.Add(singleArray5);
                    }
                    else
                    {
                        singleArrays3.RemoveAt(singleArrays3.Count - 1);
                        singleArray = new float[] { (float)num1, (float)num2, default(float) };
                        singleArrays.Add(singleArray);
                        string str4 = str.Substring(num1, num2 - num1);
                        QQLineBreaker.addLine(strs, str4);
                        num2--;
                        float[] singleArray7 = new float[str4.Length << 1];
                        int num10 = 0;
                        foreach (float[] singleArray8 in singleArrays3)
                        {
                            singleArray7[num10] = singleArray8[0];
                            singleArray7[num10 + 1] = singleArray8[1];
                            num10 = num10 + 2;
                        }
                        singleArrays1.Add(singleArray7);
                    }
                }
                if (flag3 || flag1)
                {
                    num1 = -1;
                    widthChar = 0f;
                    singleArrays3.Clear();
                }
                else
                {
                    widthChar = widthChar + single;
                    if (num2 == str.Length - 1)
                    {
                        singleArray = new float[] { (float)num1, (float)(num2 + 1), default(float) };
                        singleArrays.Add(singleArray);
                        string str5 = str.Substring(num1, num2 + 1 - num1);
                        QQLineBreaker.addLine(strs, str5);
                        float[] singleArray9 = new float[str5.Length << 1];
                        int num11 = 0;
                        foreach (float[] singleArray10 in singleArrays3)
                        {
                            singleArray9[num11] = singleArray10[0];
                            singleArray9[num11 + 1] = singleArray10[1];
                            num11 = num11 + 2;
                        }
                        singleArrays1.Add(singleArray9);
                    }
                }
                num2++;
            }
        }

        public static double GetCharHeight(char c, int fontSize)
        {
            TextBlock textBlock = new TextBlock();
            string str = new string(c, 1);
            textBlock.FontSize = (double)fontSize;
            textBlock.Text = str;
            return textBlock.ActualHeight;
        }

        private static double GetCharWidth(char c, int fontSize)
        {
            TextBlock textBlock = new TextBlock();
            string str = new string(c, 1);
            textBlock.FontSize = (double)fontSize;
            textBlock.Text = str;
            return textBlock.ActualWidth;
        }

        private static double GetTextWidth(string text, int fontSize)
        {
            TextBlock textBlock = new TextBlock()
            {
                FontSize = (double)fontSize,
                Text = text
            };
            return textBlock.ActualWidth;
        }

        public static float getWidthChar(char c)
        {
            float textWidth = (float)QQLineBreaker.GetTextWidth("中", QQLineBreaker.curFontSize);
            float single = 0f;
            single = (c <= 'ÿ' || c == '“' || c == '”' || c == '‘' || c == '’' || c == '…' ? (float)QQLineBreaker.GetCharWidth(c, QQLineBreaker.curFontSize) : textWidth);
            return single;
        }

        public static bool isAddLineEndSpecialChars(char c)
        {
            bool flag = false;
            int num = 0;
            while (num < (int)QQLineBreaker.ADD_LINE_END_CHARS.Length)
            {
                if (c != QQLineBreaker.ADD_LINE_END_CHARS[num])
                {
                    num++;
                }
                else
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        private static bool isChineseChar(char c)
        {
            if (c <= 'ÿ' || c == '“' || c == '”' || c == '‘' || c == '’')
            {
                return false;
            }
            return c != '…';
        }

        public static bool isNewLineSpecialChars(char c)
        {
            bool flag = false;
            int num = 0;
            while (num < (int)QQLineBreaker.NEWLINE_SPECIAL_CHARS.Length)
            {
                if (c != QQLineBreaker.NEWLINE_SPECIAL_CHARS[num])
                {
                    num++;
                }
                else
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        public static bool isPunctuationChars(char c)
        {
            bool flag = false;
            int num = 0;
            while (num < (int)QQLineBreaker.PUNCTUATION_CHARS.Length)
            {
                if (c != QQLineBreaker.PUNCTUATION_CHARS[num])
                {
                    num++;
                }
                else
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        public static bool isPunctuationLeftChars(char c)
        {
            bool flag = false;
            int num = 0;
            while (num < (int)QQLineBreaker.PUNCTUATION_LEFT_CHARS.Length)
            {
                if (c != QQLineBreaker.PUNCTUATION_LEFT_CHARS[num])
                {
                    num++;
                }
                else
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        public static bool isPunctuationMiddleChars(char c)
        {
            bool flag = false;
            int num = 0;
            while (num < (int)QQLineBreaker.PUNCTUATION_MIDDLE_CHARS.Length)
            {
                if (c != QQLineBreaker.PUNCTUATION_MIDDLE_CHARS[num])
                {
                    num++;
                }
                else
                {
                    flag = true;
                    break;
                }
            }
            return flag;
        }

        public IBookBuff TestFile()
        {
            string str = FIleUtility.ReadFile("test_unicode1.txt");
            IBookBuff bookBuff = new IBookBuff()
            {
                mText = str
            };
            int length = str.Length;
            QQLineBreaker.breakText(bookBuff, 400);
            List<float[]> singleArrays = bookBuff.mLinesNum;
            List<float[]> singleArrays1 = bookBuff.mLineXY;
            List<string> strs = bookBuff.mLines;
            return bookBuff;
        }
    }
}

